let express = require('express');
const router = express.Router();

const storageService = require('../service/storageService');

// setItem
router.post('/setItem', async function(req, res, next) {
    let uid = req.jwtResult.data.uid;
    let key = req.body.key;
    let value = req.body.value || '';
    if (!key) {
        return res.status(400).json({ code: 400, message: 'key 不能为空' });
    }
    storageService.setItem(uid, key, value).then((result) => {
        res.status(result.code).json(result);
    }).catch((err) => {
        res.status(500).json((new Response()).fail("获取计划列表失败"));
    });
});

// getItem
router.post('/getItem', async function(req, res, next) {
    let uid = req.jwtResult.data.uid;
    let key = req.body.key;
    if (!key) {
        return res.status(400).json({ code: 400, message: 'key 不能为空' });
    }
    storageService.getItem(uid, key).then((result) => {
        res.status(result.code).json(result);
    }).catch((err) => {
        res.status(500).json((new Response()).fail("获取计划列表失败"));
    });
});

// removeItem
router.post('/removeItem', async function(req, res, next) {
    let uid = req.jwtResult.data.uid;
    let key = req.body.key;
    if (!key) {
        return res.status(400).json({ code: 400, message: 'key 不能为空' });
    }
    storageService.removeItem(uid, key).then((result) => {
        res.status(result.code).json(result);
    }).catch((err) => {
        res.status(500).json((new Response()).fail("获取计划列表失败"));
    });
});

module.exports = router;